package org.hbbTlias2025.controller;

import lombok.extern.slf4j.Slf4j;
import org.hbbTlias2025.anno.LogOpreation;
import org.hbbTlias2025.pojo.ClassQueryParam;
import org.hbbTlias2025.pojo.Clazz;
import org.hbbTlias2025.pojo.Result;
import org.hbbTlias2025.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @GetMapping
    public Result getClazz(ClassQueryParam classQueryParam){
        try {
            log.info("查询所有班级信息" + classQueryParam);
            return Result.success(clazzService.findAllClass(classQueryParam));
        } catch (Exception e){
            return Result.error("查询班级异常我去" + e.getMessage());
        }
    }

    @PostMapping
    @LogOpreation
    public Result addClazz(@RequestBody Clazz clazz){
        try {
            log.info("添加一个班级信息__" + clazz);
            return Result.success(clazzService.addOne(clazz));
        } catch (Exception e){
            return Result.error("添加失败我去" + e.getMessage());
        }
    }


    @GetMapping("/{id}")
    public Result getOneClassById(@PathVariable Integer id){
        try {
            log.info("查询一个班级我去嘿嘿 +" + id.toString());
            return Result.success(clazzService.getOneClassById(id));
        } catch (Exception e){
            return Result.error("查询班级异常我去" + e.getMessage());
        }
    }

    @PutMapping
    @LogOpreation
    public Result updateOne(@RequestBody Clazz clazz){
        try {
            log.info("修改一个班级信息嘿嘿嘿" + clazz);
            clazzService.updateOne(clazz);
            return Result.success();
        } catch (Exception e){
            return Result.error("修改失败我去" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @LogOpreation
    public Result deleteOne(@PathVariable Integer id){
        try {
            log.info("删除一个班级信息" + id.toString());
            if(clazzService.deleteOne(id)) {
                return Result.success();
            }
            return Result.error("删除失败,这个班级存在学生。");


        } catch (Exception e){
            return Result.error("删除失败我去" + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result getAllClazzName(){
        try {
            log.info("查询所有班级信息");
            return Result.success(clazzService.getAllClassName());
        } catch (Exception e){
            return Result.error("查询所有班级失败" + e.getMessage());
        }
    }

}
